﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0783 //2011.02.16 //Begin
namespace InfoJetSoft.Service.Worker
{

    class XSNViewFormatScreenTipAttributesWorker
	{
        private static char[] AttrSeparator = { ';' };
        private static char[] StyleNameValueSeparator = { ':' };
        private static char[] HtmlNameValueSeparator = { '=' };
		
		public static void formatScreenTipAttributes(View view)
		{
            bool enableScreenTipAttributes = ConfigUtil.EnableScreenTipAttributes();
            bool enableScreenTipControlJsonXmlFilter = ConfigUtil.EnableScreenTipControlJsonXmlFilter();
            if (enableScreenTipAttributes || enableScreenTipControlJsonXmlFilter)
            {
                string styleAttrFilter = NamespaceUtil.toFullName(view.XslPrefix, "attribute" ) + "[@name='style']";
                XmlNodeList titleAttrList = view.Doc.SelectNodes("//@title");
                foreach (XmlAttribute titleAttr in titleAttrList)
                {
                    string title = titleAttr.Value;
                    if (enableScreenTipAttributes)
                    {
                        //Style Attributes. XSLT {{}} Escape
                        int left = title.IndexOf("{{");
                        int right = title.IndexOf("}}");
                        if (left >= 0 && right > 0 && right > left)
                        {
                            string content = title.Substring(left + 2, right - left - 2);
                            string first = title.Substring(0, left);
                            string second = title.Substring(right + 2, title.Length - right - 2);
                            title = first + second;
                            XmlElement styleAttrElement = DOMUtil.selectANode(titleAttr.OwnerElement, styleAttrFilter, view.NsResolver) as XmlElement;
                            if (styleAttrElement != null)
                            {
                                if (styleAttrElement.FirstChild is XmlText)
                                {
                                    XmlText text = styleAttrElement.FirstChild as XmlText;
                                    text.InnerText = text.InnerText + ";" + content + ";";
                                }
                            }
                            else
                            {
                                XmlAttribute styleAttr = titleAttr.OwnerElement.GetAttributeNode("style");
                                if (styleAttr != null)
                                {
                                    styleAttr.Value = styleAttr.Value + ";" + content + ";";
                                }
                                else
                                {
                                    titleAttr.OwnerElement.SetAttribute("style", content);
                                }
                            }
                        }
                        //Html Attributes. <>.
                        left = title.IndexOf("<");
                        right = title.IndexOf(">");
                        if (left >= 0 && right > 0 && right > left)
                        {
                            string content = title.Substring(left + 1, right - left - 1);
                            //CID 0855 //2011.04.14 //Begin
                            List<ScreenTipHtmlAttr> attrs = ScreenTipHtmlAttr.Parse(content);
                            foreach (ScreenTipHtmlAttr attr in attrs)
                            {
                                titleAttr.OwnerElement.SetAttribute(attr.Name, attr.Value);
                            }
                            //CID 0855 //2011.04.14 //End
                            string first = title.Substring(0, left);
                            string second = title.Substring(right + 1, title.Length - right - 1);
                            title = first + second;
                        }
                    }
                    //CID 0792 //2011.02.16 //Begin
                    if (enableScreenTipControlJsonXmlFilter)
                    {
                        //Filter Attributes. [].
                        int left = title.IndexOf("[");
                        int right = title.IndexOf("]");
                        if (left >= 0 && right > 0 && right > left)
                        {
                            string content = title.Substring(left + 1, right - left - 1);
                            titleAttr.OwnerElement.SetAttribute("InfoJet_ControlJsonXmlFilter", content);
                            string first = title.Substring(0, left);
                            string second = title.Substring(right + 1, title.Length - right - 1);
                            title = first + second;
                        }
                    }
                    //CID 0792 //2011.02.16 //End
                    titleAttr.Value = title;
                }
            }
		}
	}

    //CID 0855 //2011.04.14 //Begin
    class ScreenTipHtmlAttr
    {
        public string Name { get; set; }
        public string Value { get; set; }

        public static List<ScreenTipHtmlAttr> Parse(string content)
        {
            List<ScreenTipHtmlAttr> attrs = new List<ScreenTipHtmlAttr>();

            char[] chars = content.Trim().ToCharArray();
            StringBuilder name = new StringBuilder();
            StringBuilder value = new StringBuilder();
            bool inName = true;
            bool equalFound = false;
            bool quote1Found = false;
            bool quote2Found = false;
            foreach (char chr in chars)
            {
                if (equalFound)
                {
                    if (chr == ' ')
                    {
                        continue;
                    }
                    else if (chr == '\'')
                    {
                        quote1Found = true;
                    }
                    else if (chr == '"')
                    {
                        quote2Found = true;
                    }
                    else
                    {
                        value.Append(chr);
                    }
                    equalFound = false;
                }
                else
                {
                    if (chr == '=')
                    {
                        equalFound = true;
                        inName = false;
                    }
                    else if (inName)
                    {
                        name.Append(chr);
                    }
                    else if (quote1Found)
                    {
                        if (chr == '\'')
                        {
                            ScreenTipHtmlAttr attr = new ScreenTipHtmlAttr();
                            attr.Name = name.ToString().Trim();
                            attr.Value = value.ToString().Trim();
                            attrs.Add(attr);
                            name.Remove(0, name.Length);
                            value.Remove(0, value.Length);
                            inName = true;
                            equalFound = false;
                            quote1Found = false;
                            quote2Found = false;
                        }
                        else
                        {
                            value.Append(chr);
                        }
                    }
                    else if (quote2Found)
                    {
                        if (chr == '"')
                        {
                            ScreenTipHtmlAttr attr = new ScreenTipHtmlAttr();
                            attr.Name = name.ToString().Trim();
                            attr.Value = value.ToString().Trim();
                            attrs.Add(attr);
                            name.Remove(0, name.Length);
                            value.Remove(0, value.Length);
                            inName = true;
                            equalFound = false;
                            quote1Found = false;
                            quote2Found = false;
                        }
                        else
                        {
                            value.Append(chr);
                        }
                    }
                    else
                    {
                        if (chr == ' ')
                        {
                            ScreenTipHtmlAttr attr = new ScreenTipHtmlAttr();
                            attr.Name = name.ToString().Trim();
                            attr.Value = value.ToString().Trim();
                            attrs.Add(attr);
                            name.Remove(0, name.Length);
                            value.Remove(0, value.Length);
                            inName = true;
                            equalFound = false;
                            quote1Found = false;
                            quote2Found = false;
                        }
                        else
                        {
                            value.Append(chr);
                        }
                    }
                }
            }

            if (name.Length > 0)
            {
                ScreenTipHtmlAttr attr = new ScreenTipHtmlAttr();
                attr.Name = name.ToString().Trim();
                attr.Value = value.ToString().Trim();
                attrs.Add(attr);
            }

            return attrs;
        }
    }
    //CID 0855 //2011.04.14 //End
}
//CID 0783 //2011.02.16 //End